#!/bin/sh
# - nr_instances
# - cpu_node_bind
# - mem_node_bind

. $LKP_SRC/lib/reproduce-log.sh
. $LKP_SRC/lib/numactl.sh

## wrapper to run other other tests

program=$1
program_dir=$(dirname $0)

shift

setup_cgroup()
{
	[ -z "$1" ] && return
	[ "$1" = 'nocg' ] && return
	[ -n "$CGROUP_MNT" ] || return

	if [ "$cgroup_ver" = 1 ]; then
		local subsys_tasks
		for subsys_tasks in $CGROUP_MNT/*/$1/tasks
		do
			log_eval "echo \$\$ > '$subsys_tasks'"
		done
	elif [ "$cgroup_ver" = 2 ]; then
		log_eval "echo \$\$ > $CGROUP_MNT/$1/cgroup.procs"
	fi
}

if [ -n "$nr_instances" ]; then
	: "${cgroups:=nocg}"
	parse_numa_node_binding "$cpu_node_bind" "$mem_node_bind"

	i=1
	while [ "$i" -le "$nr_instances" ]; do
		for cg in $cgroups; do
			export instance_id=$i
			export instance_cgroup=$cg
			setup_cgroup "$cg"
			numa_bind=$(numa_node_binding "$i")
			$numa_bind $program_dir/$program "$@" &
			to_wait="$to_wait $!"
			i=$((i+1))
			[ "$i" -gt "$nr_instances" ] && break
		done
	done
	wait $to_wait
else
	exec $program_dir/$program "$@"
fi
